package com.wandian.demo.other 
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.text.TextField;
	import flash.utils.Timer;
	
	import utils.events.GameTimeEvent;
	/**
	 * @author silva
	 * 实现方法
	 * regTimer(_timeItem:String, _timeNum:int, _timeTxt:TextField):void
	 * //注册了时间后 倒数完会自己删除注册的对象 排除在队列中
	 * 游戏计时器管理器
	 */
	public class GameTime extends Sprite {
		private var timer:Timer;
		public static var TIMEOVER:String = "GameTime::timerover";
		private static var _class:GameTime;
		private var timeListArr:Array = new Array();
		public function GameTime():void {
			if (_class != null) throw Error("请不要实例化此类");
			return;
		}
		/**
		 * 此类是一个单例 本想是做成静态的 因为静态类无法广播事件
		 * @return GameTime
		 */
		public static function getInstance():GameTime {
			if (_class == null) return _class = new GameTime();
			return _class;
		}
		/**
		 * 注册计时器
		 */
		private function initTimer():void {
			if (timer == null) {
				timer = new Timer(1000);
			}
			if (timer.running == false ) {
				timer.addEventListener(TimerEvent.TIMER, timerHandler);
				timer.start()
			}
		}
		/**
		 * 增加某个队列 请注意标签符的唯一性 
		 * @param _timeItem 注册计时的标识符 
		 * @param _timeNum 时间 单位为秒 
		 * @param _timeTxt 显示转换后时间的文本框 
		 */
		public function regTimer(_timeItem:String=null, _timeNum:int=0, _timeTxt:TextField=null,_fun:Function=null):void {
			var len:int = timeListArr.length;
			var regBoo:Boolean = false;
			for (var i:int = 0; i < len; i++) {
				if (timeListArr[i]["timeItem"] == _timeItem) {
					//已注册的计时器
					regBoo = true;
					break;
				}
			}
			if (regBoo==false) {
				//没有注册就写入计时队列中
				timeListArr.push( { timeItem:_timeItem, timeNum:_timeNum, textfiled:_timeTxt,fun:_fun } );
			}
			initTimer();
		}
		/**
		 * 删除某个队列
		 * 在队列中 删除对应注册计时标签识符
		 * @param _timeItem 注册计时的标识符 
		 */
		public function removeTimer(_timeItem:String):void {
			var len:int = timeListArr.length;
			for (var i:int = 0; i < len; i++) {
				//trace(timeListArr[i]["timeItem"] +" - "+_timeItem);
				if (timeListArr[i]["timeItem"] == _timeItem) { 
					var temArr:Array = timeListArr.splice(i, 1);
					
					break;
				}
			}
		}
		/**
		 * 运行计时器
		 */
		private function runTimer():void {
			var len:int = timeListArr.length;
			trace("计时列队 - " + len);
			if (len == 0) {
				stopTimer();
				return;
			}
			for (var i:int = 0; i < len; i++) {
				var temNum:int = int(timeListArr[i]["timeNum"]);
				temNum--;
				if (timeListArr[i]["textfiled"] != null) {
					timeListArr[i]["textfiled"].text = ConvertTime(temNum);					
				}
				timeListArr[i]["timeNum"] = temNum;
				///
				try {
					if (timeListArr[i]["fun"] != null) {
						var funs:Function = timeListArr[i]["fun"];
						funs();
					}
				}catch (e:Error) {
					trace(e.toString());
				}
				//trace(fun)
				if (temNum == 0) {
					dispatchEvent(new GameTimeEvent(TIMEOVER,timeListArr[i]["timeItem"]));
					removeTimer(timeListArr[i]["timeItem"]);
					if (len == 1) {
						stopTimer();
					}
				}
			}
		}
		/**
		 * 停止计时器
		 */
		private function stopTimer():void {
			trace("停止计时")
			timer.stop();
			timer.removeEventListener(TimerEvent.TIMER, timerHandler);
		}
		/**
		 * TimerEvent事件侦听实现
		 * @param e
		 */
		private function timerHandler(e:TimerEvent):void {
			runTimer();
		}
		/**
		 * 将秒数据格式转化为 00:00:00格式
		 * @param _timeNum
		 * @return String 
		 */
		private function ConvertTime(_timeNum:int):String {
			return (int(_timeNum / 3600).toString() + ":" + int(_timeNum % 3600 / 60).toString() + ":"+ int(_timeNum % 3600 % 60).toString() + "");
		}
	}
}